<?php
namespace Home\Controller;
use Think\Controller;
use Think\Upload;


class MainController extends Controller {


    //
    public function __construct()
    {
        parent::__construct();
        $this -> assign('tplpath' , __ROOT__.'/'.C('VIEW_PATH').THEME);
    }
    
    /*
     * 输出正确.
     */
    protected function fsucces($info){
        $this -> fsend($info,true);
    }

    /*
     * 输出错误.
     */
    protected function ferror($info){
        $this -> fsend($info,false);
    }

    /*
     * json输出
     */
    protected function fsend($info,$type){
        $allinfo['info'] = $type==true ? $info : "";
        $allinfo['status'] = $type;
        $allinfo['error'] = $type==true ? "" : $info;
        echo json_encode($allinfo);
        die();
    }
    
    /**
     * print
     */
    protected function flyd($info,$type=0){
        header("Content-type:text/html;charset=utf-8");
        print_r('<pre>');
        print_r($info);
        print_r('</pre>');
        if($type) die();
    }


    /*
     * insert数据辅助转换
     */
    protected function insertchange($info){
        $val = explode('&&' , $info);
        foreach($val as $v){
            $infoarray = explode('=' , $v);
            if($infoarray[0])
                $resultarray[$infoarray[0]] = $infoarray[1];
        }
        return json_encode($resultarray);
    }


    /*
     * file上传选择
     * Folder文件目录 
     * addtype添加的类型 [默认other]
     */
    public function fileUpload($Folder='file' , $addtype="other" , $file=false){
        $FileUploadConfig = C('FileUpload');
        $upload = new Upload();                // 实例化上传类
        $upload->maxSize = $FileUploadConfig['maxSize'];            // 设置附件上传大小 30MB
        $upload->exts = explode(',',$FileUploadConfig['exts']); // 设置附件上传类型
        $upload->rootPath = $FileUploadConfig['rootPath'];          // 设置附件上传根目录
        $upload->subName = $Folder . '/' . $FileUploadConfig['subName'];      // 设置附件上传（子）目录
        $info = $upload->upload();
        if (!$info) {
            $res = $upload->getError();
            $addfile['status'] = false;
            $addfile['msg'] = $res;
        } else {
            $addfile['status'] = true;
            foreach($info as $v){
                $info = $v['file'];
                $filepath = $FileUploadConfig['rootPath'].$v['savepath'].$v['savename'];
                $addfile['filename'] = $v['name'];
                $addfile['savename'] = $v['savename'];
                $addfile['savepath'] = $v['savepath'];
                $addfile['allpath'] = $filepath;
                $addfile['size'] = intval($v['size']/1024) .'KB';
                $addfile['type'] = $v['type'];
                $addfile['addtype'] = $addtype;
                $addfile['addtime'] = time();
                
                $extendTmp = explode('.' , $v['savename']);
                $addfile['extend'] = $extendTmp[count($extendTmp)-1];
                $newId = M('file') -> add($addfile);
                $addfile['id'] = $newId;
            }            
        }
        return $addfile;
    }
    

    /*
     * file批量上传选择
     * Folder文件目录
     * addtype添加的类型 [默认other]
     */
    public function fileUploads($Folder='file' , $addtype="other"){
        $FileUploadConfig = C('FileUpload');
        $upload = new Upload();                // 实例化上传类
        $upload->maxSize = $FileUploadConfig['maxSize'];            // 设置附件上传大小 30MB
        $upload->exts = explode(',',$FileUploadConfig['exts']); // 设置附件上传类型
        $upload->rootPath = $FileUploadConfig['rootPath'];          // 设置附件上传根目录
        $upload->subName = $Folder . '/' . $FileUploadConfig['subName'];      // 设置附件上传（子）目录
        $info = $upload->upload();
        if (!$info) {
            $res = $upload->getError();
            $addfile['status'] = false;
            $addfile['msg'] = $res;
        } else {
            $addfile['status'] = true;
            foreach($info as $k => $v){
                $result = array();
                $filepath = $FileUploadConfig['rootPath'].$v['savepath'].$v['savename'];
                $result['filename'] = $v['name'];
                $result['savename'] = $v['savename'];
                $result['savepath'] = $v['savepath'];
                $result['allpath'] = $filepath;
                $result['size'] = intval($v['size']/1024) .'KB';
                $result['type'] = $v['type'];
                $result['addtype'] = $addtype;
                $result['addtime'] = time();

                $extendTmp = explode('.' , $v['savename']);
                $result['extend'] = $extendTmp[count($extendTmp)-1];
                $newId = M('file') -> add($result);
                $result['id'] = $newId;
                $addfile['list'][$k] = $result;
            } 
        } 
        return $addfile;
    }
    
    
    /*
     * file上传选择
     *
    public function fileUpload($type = 'file', $ext = false){
        $FileUploadConfig = C('FileUpload');
        $upload = new Upload();                // 实例化上传类
        $upload->maxSize = $FileUploadConfig['maxSize'];            // 设置附件上传大小 30MB
        $upload->exts = explode(',',$FileUploadConfig['exts']); // 设置附件上传类型
        $upload->rootPath = $FileUploadConfig['rootPath'];          // 设置附件上传根目录
        $upload->subName = $type . '/' . $FileUploadConfig['subName'];      // 设置附件上传（子）目录
        $info = $upload->upload();
        if (!$info) {
            $res = $upload->getError();
        } else {
            $info = $info['file'];
            $filepath = $FileUploadConfig['rootPath'].$info['savepath'].$info['savename'];
            $addfile['filename'] = $info['name'];
            $addfile['savename'] = $info['savename'];
            $addfile['savepath'] = $info['savepath'];
            $addfile['allpath'] = $filepath;
            $addfile['size'] = intval($info['size']/1024) .'KB';
            $addfile['type'] = $info['type'];
            $newId = M('file') -> add($addfile);

            $result['file_id'] = $newId;
            $result['file_name'] = $info['name'];
            $result['code'] = 1;
            $result['msg'] = true;
        }
        echo json_encode($result);
        die();
    }

    /*
     * 用户检测权限
     */
    protected function userauth($type='html'){
        $userinfo = session('userinfo');
        //级别1==管理员
        if($userinfo['type']==1){
            return true;
        //级别2==用户
        }else{
            if($type=='json')
                $this -> ferror("您无权操作");
            else
                $this -> error("您无权操作");
            die();
        }
    }


    /*
     *
     */
}
